﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SurvivalStrategy.Console;

namespace SurvivalStrategy
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public static bool DebugMode = false; 

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static ConsoleBar Terminal;
        public static ConsoleBox ConsoleBox;
        SpriteFont font1;
        private Model gameModel;
        private View view;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        
        protected override void Initialize()
        {
            gameModel = new Model();
            view = new View();
            base.Initialize();
        }

      
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);


            Terminal = new ConsoleBar(this, Content.Load<Texture2D>("consoleBackground"), GraphicsDevice.Viewport.Width);
            ConsoleBox = new ConsoleBox(this, new Vector2(0, GraphicsDevice.Viewport.Height));

            view.SetRender(spriteBatch);

            LoadTextures();
            LoadElementsType();

            World world = new World();
                
            #region TymczasowaMapa
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    world.AddElement(new WorldData.WorldElement(3, new Vector3(i * 256, -5, j * 256), new Vector3(0, 0, 0), new Vector3(256, 0, 256), false, null));
           
            for (int i = 0; i < 15; i++)//sciany lewa i prawa
            {
                world.AddElement(new WorldData.WorldElement(1, new Vector3(768, 0, 32 * i), new Vector3(0, 0, 0), new Vector3(32, 1, 32), true, new WorldData.Colliders.RectangleCollider(Vector2.Zero, new Vector2(32,32))));
                world.AddElement(new WorldData.WorldElement(1, new Vector3(0, 0, 32 * i), new Vector3(0, 0, 0), new Vector3(32, 1, 32), true, new WorldData.Colliders.RectangleCollider(Vector2.Zero, new Vector2(32, 32))));
            }
            
            for (int i = 0; i < 24; i++)//gora i dol
            {
                world.AddElement(new WorldData.WorldElement(1, new Vector3(32 * i, 0, 0), new Vector3(0, 0, 0), new Vector3(32, 1, 32), true, new WorldData.Colliders.RectangleCollider(Vector2.Zero, new Vector2(32, 32))));
                world.AddElement(new WorldData.WorldElement(1, new Vector3(32 * i, 0, 448), new Vector3(0, 0, 0), new Vector3(32, 1, 32), true, new WorldData.Colliders.RectangleCollider(Vector2.Zero, new Vector2(32, 32))));
            } 

            #endregion

            #region TymczasoweObiekty
            WorldData.WorldDynamicElement apple = new WorldData.WorldDynamicElement(4, new Vector3(50, 1, 250), Vector3.Zero, new Vector3(32, 1, 32), false, new WorldData.Colliders.Collider());
            apple.AddComponent<WorldData.ItemComponents.Eatable>();
            world.AddElement(apple); 
            WorldData.WorldDynamicElement apple2 = new WorldData.WorldDynamicElement(4, new Vector3(50, 1, 130), Vector3.Zero, new Vector3(32, 1, 32), false, new WorldData.Colliders.Collider());
            apple2.AddComponent<WorldData.ItemComponents.Eatable>();
            world.AddElement(apple2);
            WorldData.WorldDynamicElement apple3 = new WorldData.WorldDynamicElement(4, new Vector3(100, 1, 160), Vector3.Zero, new Vector3(32, 1, 32), false, new WorldData.Colliders.Collider());
            apple3.AddComponent<WorldData.ItemComponents.Eatable>();
            world.AddElement(apple3);
            #endregion

            world.AddElement(new WorldData.WorldElement(new Vector3(200, 5, 00), Vector3.Zero, 0));
            world.AddElement(new WorldData.WorldElement(new Vector3(30,5, 301), Vector3.Zero, 1));


            world.AddElement(new WorldData.WorldElement(new Vector3(350, 5, 101), Vector3.Zero, 1));
            world.AddElement(new WorldData.WorldElement(new Vector3(80, 5, 221), Vector3.Zero, 1));
            world.AddElement(new WorldData.WorldElement(new Vector3(550, 5, 301), Vector3.Zero, 1));

            gameModel.LoadWorld(world);
            gameModel.AddEntity();

            font1 = Content.Load<SpriteFont>("Font1");
        }

        protected override void UnloadContent()
        {
            FilesHelper.SaveWorld(gameModel.world, "Save\\");
        }
       
        protected override void Update(GameTime gameTime)
        {
            Terminal.Update(gameTime, Keyboard.GetState());
            ConsoleBox.Update(gameTime);
            gameModel.Update(gameTime);
            view.SetGraphicsData(gameModel.GraphicsData);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            view.Draw(gameTime);
            Terminal.Draw(gameTime, spriteBatch, font1);
            ConsoleBox.Draw(gameTime, spriteBatch, font1);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        private void LoadTextures()
        {
            view.AddTexture(0, Content.Load<Texture2D>("DebugBlock"));
            view.AddTexture(1, Content.Load<Texture2D>("wall"));
            view.AddTexture(2, Content.Load<Texture2D>("test"));
            view.AddTexture(3, Content.Load<Texture2D>("grass"));
            view.AddTexture(4, Content.Load<Texture2D>("big_apple"));
            view.AddTexture(5, Content.Load<Texture2D>("tree"));
            view.AddTexture(6, Content.Load<Texture2D>("drzewo1"));
            view.AddTexture(7, Content.Load<Texture2D>("drzewo2"));
            view.AddTexture(8, Content.Load<Texture2D>("cos"));
            view.AddTexture(9, Content.Load<Texture2D>("kloda"));
            view.AddTexture(10, Content.Load<Texture2D>("krzak"));
            view.AddTexture(11, Content.Load<Texture2D>("pien"));
            view.AddTexture(12, Content.Load<Texture2D>("kwiat"));
        }

        private void LoadElementsType()
        {
            World.ElementsTypes.Add(0, new WorldData.ElementType(0, "Tree", 6, new Vector3(117, 0, 110), true, new WorldData.Colliders.RectangleCollider(25, 60, 50, 40)));
            World.ElementsTypes.Add(1, new WorldData.ElementType(1, "Tree2", 7, new Vector3(117, 0, 110), true, new WorldData.Colliders.RectangleCollider(25, 60, 50, 40)));
            World.ElementsTypes.Add(2, new WorldData.ElementType(2, "Kwiat", 12, new Vector3(28, 0, 32), false, new WorldData.Colliders.RectangleCollider(0,0,28,32)));
        }
    }
}
